#ifndef  _MAIN_H_
#define  _MAIN_H_

#include "../define/define.h"
#include "../task/task_man.h"
#include "route.h"

#include <sys/wait.h>

void Sleep(uint32 seconds)
{
	struct timespec ts, rmts;
	
	ts.tv_sec = seconds/1000;
	ts.tv_nsec = (seconds%1000)*1000000;
	while(1)
	{
		int ret = nanosleep(&ts, &rmts);
		if(ret == 0) 
		{
			break;
		}

		int sleepErr = errno;
		if(ret == -1 && sleepErr == EINTR)
		{
			ts = rmts;
		}
	}
}

int main(int argc, char** argv)
{	
   UDPServer* m_udp = new UDPServer(); 
   task_man*  m_task = new task_man(m_udp);

   route*     m_route = new route();
   logfile*   m_log   = new logfile("main");

   ip_node node[128];

   m_udp->init(G_Port,m_task);
   
   printf("trans server is start. \r\n");

   m_route->reload();
   memset(node,0,sizeof(ip_node) * 128);
   m_route->get_all_task(node,128);

   for(uint32 i=0; i<128; i++)
   {
	   if(node[i].l_port == 0)
		   break;

	   m_task->add_task(node[i]);
   }


   uint32 count = 1;
   while(1)
   {
       if(count % 300 == 0)
	   {
		   m_route->reload();
           memset(node,0,sizeof(ip_node) * 128);
		   m_route->get_all_task(node,128);

		   /*for(uint32 i=0; i<128; i++)
		   {
			   if(node[i].l_port == 0)
				   break;

			   m_task->add_task(node[i]);
		   }*/
	   }
	   
	   count++;
	   Sleep(1000);
   }

   return 0;
}

#endif	
